<?php
/**
 * @brief
 *
 * @license		http://www.gnu.org/licenses/gpl-3.0.txt GNU General Public License
 * @copyright	2008 David Singer
 * @author		David Singer <david@ramaboo.com>
 * @version		1.0.0
 */

require_once('../../includes/common.php');

try {
	//$command = 'dig @' . BIND_IP;
	
	$command = 'dig';
	
	$text = '';
	// check requried params
	if (!isset($_POST['type'])) {
		throw new Exception("Type is required.");
	}
	
	$type = strtoupper(trim($_POST['type']));
	
	
	if (Record::isSupportedType($type)) {
		$command .= " -t $type";
	} else if ($type == null) {
		// do nothing
	} else {
		throw new Exception("Type $type is not valid.");
	}
	
	
	if (!isset($_POST['domain'])) {
		throw new Exception("Domain is required.");
	}
	$domain = strtolower(trim($_POST['domain']));
	
	// make sure domain is not blank
	if ($domain == null) {
		throw new Exception("Domain is required.");
	}
	
	
	// make sure domain contains only legal characters
	if (!preg_match('/^[a-zA-Z0-9\._-]+$/', $domain)) {
		throw new Exception("Domain $domain contains illegal characters.");
	} else {
		$command .= " $domain";
	}
	
	exec($command, $exec_output, $return_var);
	
	if ($return_var) {
		// there was an error
		throw new Exception("Dig command $command failed.");
	} else {
		// command worked, convert array of output to html
		foreach ($exec_output as $key=>$value) {
			$text .= $value . "\n";
		}
	}
	
	
	$json = array(); 
	
	$json['success'] = true;
	$json['text'] = $text;
	$json['msg'] = "Dig command $command completed.";
} catch (Exception $e) {
	$json['success'] = false;
	$json['msg'] = $e->getMessage();
}

$output = json_encode($json);
header_no_cache(strlen($output), 'application/json');
echo $output;
?>